1
|
|
|
// jshint esversion: 8, -W030 |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
byteToHumanFileSize |
5
|
|
|
Convert bytes size to human readable format |
6
|
|
|
parameters |
7
|
|
|
bytes (integer) - bytes to convert |
8
|
|
|
si (boolean) - use standard international units |
9
|
|
|
*/ |
10
|
|
|
function byteToHumanFileSize(bytes, si = true) { |
11
|
|
|
var thresh = si ? 1000 : 1024; |
12
|
|
|
if (Math.abs(bytes) < thresh) { |
13
|
|
|
return bytes + ' B'; |
14
|
|
|
} |
15
|
|
|
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']; |
16
|
|
|
var u = -1; |
17
|
|
|
do { |
18
|
|
|
bytes /= thresh; |
19
|
|
|
++u; |
20
|
|
|
} while (Math.abs(bytes) >= thresh && u < units.length - 1); |
21
|
|
|
return bytes.toFixed(1) + ' ' + units[u]; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/* |
25
|
|
|
msToHumanTime |
26
|
|
|
Convert a given milliseconds to string in human readable format |
27
|
|
|
parameters |
28
|
|
|
duration (integer) - milliseconds to convert |
29
|
|
|
*/ |
30
|
|
|
function msToHumanTime(duration) { |
31
|
|
|
var milliseconds = parseInt((duration % 1000) / 100), |
32
|
|
|
seconds = parseInt((duration / 1000).toFixed(6) % 60), |
33
|
|
|
minutes = parseInt((duration / (1000 * 60)).toFixed(6) % 60), |
34
|
|
|
hours = parseInt((duration / (1000 * 60 * 60)).toFixed(6) % 24), |
35
|
|
|
days = parseInt((duration / (1000 * 60 * 60 * 24)).toFixed(6) % 7), |
36
|
|
|
weeks = parseInt((duration / (1000 * 60 * 60 * 24 * 7)).toFixed(6) % 4), |
37
|
|
|
months = parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4)).toFixed(6) % 12), |
38
|
|
|
years = parseInt((duration / (1000 * 60 * 60 * 24 * 7 * 4 * 12)).toFixed(6) % 10); |
39
|
|
|
var time = ''; |
40
|
|
|
|
41
|
|
|
(years < 1) ? true: time += months + 'Y'; |
42
|
|
|
(months < 1) ? true: time += months + 'M'; |
43
|
|
|
(weeks < 1) ? true: time += weeks + 'w'; |
44
|
|
|
(days < 1) ? true: time += days + 'd'; |
45
|
|
|
(hours < 1) ? true: time += hours + 'h'; |
46
|
|
|
(minutes < 1) ? true: time += minutes + 'm'; |
47
|
|
|
(seconds < 1) ? true: time += seconds + 's'; |
48
|
|
|
(milliseconds < 125) ? true: time += milliseconds + 'ms'; |
49
|
|
|
|
50
|
|
|
(duration >= 0) ? true: time = '-'; |
51
|
|
|
|
52
|
|
|
return time; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* |
56
|
|
|
getDate |
57
|
|
|
Parse a date from a source string |
58
|
|
|
parameters |
59
|
|
|
date (string/date) - Date or String to be date parsed |
60
|
|
|
*/ |
61
|
|
|
function getDate(date) { |
62
|
|
|
if (date === null || date === '' || date === false) return undefined; |
63
|
|
|
var parsed = new Date(Date.parse(date)).toUTCString(); |
64
|
|
|
if (parsed == 'Invalid Date') { |
65
|
|
|
return date; |
66
|
|
|
} else { |
|
|
|
|
67
|
|
|
return parsed; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
module.exports = { |
72
|
|
|
byteToHumanFileSize: byteToHumanFileSize, |
73
|
|
|
msToHumanTime: msToHumanTime, |
74
|
|
|
getDate: getDate |
75
|
|
|
}; |
76
|
|
|
|